<?php

namespace app\api\services\admin\setting;

use app\api\common\service\BaseServices;
use app\api\common\traits\ServicesTrait;
use app\api\dao\admin\setting\UserDao;

/**
 * 系统用户管理
 * Class UserServices
 *
 * @package app\services\admin
 * @method deleteLog() 定期删除日志
 */
class UserServices extends BaseServices
{
    use ServicesTrait;

    /**
     * 构造方法
     * UserServices constructor.
     *
     * @param UserDao $dao
     */
    public function __construct(UserDao $dao)
    {
        $this->dao = $dao;
    }

    public function select(array $where): array
    {
        $where = array_filter($where, function ($value) {
            return $value !== '' && $value !== null;
        });
        [$page, $limit] = $this->getPageValue();
        $list  = $this->dao->selectList($where, 'id, account, real_name, roles, last_time, last_ip, status', $page, $limit, 'id ASC', [], true)->toArray();
        $count = $this->dao->count($where);

        //获得所有role_name，id为索引
        /** @var PermissionServices $service */
        $service = app()->make(\app\api\services\admin\setting\RoleServices::class);
        $roles   = $service->getColumn([], "role_name", "id");
        //循环替换
        foreach ($list as &$item) {
            $item['roles'] = implode(',', array_keys_value($roles, $item['roles'], true));
        }
        return compact('list', 'count');
    }
}
